import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @Classname DateTimeTest
 * @Description 日期类测试
 * @Date 2023/2/1 18:13
 * @Created by louzhuangxi
 */
public class DateTimeTest {

    @Test
    public void test01() {
        //用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
        long l = System.currentTimeMillis();
        System.out.println("时间戳:" + l);

        //java.util.date
        Date date1 = new Date();
        Date date2 = new Date(1675246570837L);
        System.out.println("时间戳:" + date1.getTime());
        System.out.println("当前时间:" + date1);
        System.out.println(">>时间戳:" + date2.getTime());
        System.out.println(">>当前时间:" + date2);

        //java.sql.date 与数据库中的date类型相对应，若要存储数据库，则需要先转换成此类型
        java.sql.Date date3 = new java.sql.Date(1675246570837L);
        System.out.println("时间戳:" + date3.getTime());
        System.out.println("当前时间:" + date3);

        //java.util.date与java.sql.date之间的转换
        java.sql.Date date4 = new java.sql.Date(date3.getTime());
        System.out.println("时间戳:" + date4.getTime());
        System.out.println("当前时间:" + date4);
        System.out.println("时间戳:" + date4.getTime());
        System.out.println("当前时间:" + date4);
    }

    /**
     * SimpleDateFormat的使用
     */
    @Test
    public void test02() throws ParseException {
        //将java.util.date转换成String
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = sdf.format(date);
        System.out.println(format);

        //将String转换为java.util.date
        String dateStr = "2023-01-02 17:00";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date parse = sdf1.parse(dateStr);
        System.out.println(parse);
    }

    @Test
    public void test03() throws ParseException {
        //将字符串"2020-09-08"转换为java.sql.Date
        String dateStr = "2020-09-08";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        System.out.println(sqlDate);
    }

    @Test
    public void test04() throws ParseException {
        //"三天打渔,两天晒网"，计算自2019-01-01到2022-09-02哪天打渔,哪天晒网
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2019-01-01");
        Date date2 = sdf.parse("2022-09-12");
        long timestamp = date2.getTime() - date1.getTime();
        //计算之间的天数
        int day = (int) (timestamp / (1000 * 60 * 60 * 24) + 1);
        if (day % 5 > 0 && day % 5 < 4) {
            System.out.println("这天他在打渔");
        } else {
            System.out.println("这天他在晒网");
        }
    }

    /**
     * Calendar：日历类的使用
     */
    @Test
    public void test06() {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime());
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, 22);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.DAY_OF_MONTH, 2);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //日历类转换为java.util.date
        Date date = calendar.getTime();
        System.out.println(date);
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        calendar.setTime(calendar.getTime());
        System.out.println(calendar.getTime());
    }

    /**
     * LocalDate、LocalTime、LocalDateTime的基本使用
     */
    @Test
    public void test07() {
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //LocalDateTime转换为String
        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(format);
        //String转换为LocalDateTime
        LocalDateTime parse = LocalDateTime.parse("2023-02-02 22:57:10", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(parse);
    }

    /**
     * Instant的使用
     */
    @Test
    public void test08() {
        Instant instant = Instant.now();
        System.out.println(instant);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        System.out.println(instant.getEpochSecond());
        Instant instant1 = Instant.ofEpochSecond(1675350465L);
        System.out.println(instant1);

    }

}
